package cn.kgc.day05.servlet;


import cn.hutool.json.JSONUtil;
import cn.kgc.day05.entity.Score;
import cn.kgc.day05.http.ResponseResult;
import cn.kgc.day05.service.ScoreService;
import cn.kgc.day05.service.impl.ScoreServiceImpl;
import cn.kgc.day05.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/10/14
 * @description
 */
@WebServlet("/score/layui")
public class ScoreLayUiServlet extends BaseServlet {

    private ScoreService scoreService = new ScoreServiceImpl();


    // 分页查询
    protected void scorePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取分页数据
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));

        // 调用业务层实现数据分页查询
        ResponseResult responseResult = scoreService.scorePage(page, limit);
        ResponseUtil.writeJson(responseResult, resp);

    }

    // 添加成绩
    protected void saveScore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取成绩信息
        String name = req.getParameter("name");
        String course = req.getParameter("course");
        Integer score = Integer.valueOf(req.getParameter("score"));


        Score scr = new Score();
        scr.setCourse(course);
        scr.setScore(score);
        scr.setName(name);

        // 调用业务层实现数据添加
        ResponseResult responseResult = scoreService.saveScore(scr);
        // 响应json格式数据     html
        ResponseUtil.writeJson(responseResult, resp);
    }

    // 编辑信息
    protected void editScore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取成绩信息
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String course = req.getParameter("course");
        Integer score = Integer.valueOf(req.getParameter("score"));
        Integer version = Integer.valueOf(req.getParameter("version"));

        // 封装数据
        Score scr = new Score();
        scr.setCourse(course);
        scr.setScore(score);
        scr.setName(name);
        scr.setId(id);
        scr.setVersion(version);

        // 调用业务层实现数据添加
        ResponseResult responseResult = scoreService.updateScore(scr);
        // 响应json格式数据
        ResponseUtil.writeJson(responseResult, resp);
    }

    // 删除成绩信息  batchDel
    protected void batchDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  获取删除数据的id
        String ids = req.getParameter("ids");
        // 将 json格式的数组  转换成 java 集合
        List<Integer> intIds = JSONUtil.toList(ids, Integer.class);

       ResponseResult responseResult =   scoreService.batchDel(intIds);
       ResponseUtil.writeJson(responseResult,resp);
    }



}
